package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.Label;
import cn.wolfcode.trip.base.domain.StrategyComment;
import cn.wolfcode.trip.base.domain.StrategyCommentImg;
import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.mapper.LabelMapper;
import cn.wolfcode.trip.base.mapper.StrategyCommentImgMapper;
import cn.wolfcode.trip.base.mapper.StrategyCommentMapper;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.IStrategyCommentService;
import cn.wolfcode.trip.base.util.UserContext;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class StrategyCommentServiceImpl implements IStrategyCommentService {
    @Autowired
    private StrategyCommentMapper strategyCommentMapper;
    @Autowired
    private StrategyCommentImgMapper strategyCommentImgMapper;
    @Autowired
    private LabelMapper labelMapper;

    @Override
    public PageInfo<StrategyComment> selectForList(QueryObject qo) {
        //分页
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<StrategyComment> list = strategyCommentMapper.selectForList(qo);
        return  new PageInfo<>(list);
    }

    @Override
    public void save(StrategyComment strategyComment, Long[] labels, String[] images, String myTags) {
        //如果登录了可以进行关注,否则提示去登录
        User currentUser = UserContext.getCurrentUser();
        if (currentUser == null) {
            throw new RuntimeException("请先登录");
        } else {
            //设置作者
            strategyComment.setAuthor(currentUser);
        }
        strategyComment.setCreateDate(new Date());
        strategyCommentMapper.insert(strategyComment);
        Long id = strategyComment.getId();
        //遍历images数组保存进数据库
        if (images != null) {
            for (String image : images) {
                StrategyCommentImg strategyCommentImg = new StrategyCommentImg();
                strategyCommentImg.setCommentId(id);
                strategyCommentImg.setImageUrl(image);
                strategyCommentImgMapper.insert(strategyCommentImg);
            }
        }

        //遍历labels数组存入中间表和自定义myTags存入标签表
        if (labels != null) {
            for (Long labelId : labels) {
                if (labelId != null) {
                    strategyCommentMapper.insertStrategyCommentWithLabel(id, labelId);
                }
            }
        }
        //-------
        if (myTags != null) {
            Label newLabel = new Label();
            newLabel.setContent(myTags);
            labelMapper.insert(newLabel);
            //建立定义标签
            strategyCommentMapper.insertStrategyCommentWithLabel(id, newLabel.getId());
        }

    }

    @Override
    public List<StrategyComment> selectAll() {
        return strategyCommentMapper.selectAll();
    }

    @Override
    public StrategyComment getSomeComment(Long commentId) {
        return strategyCommentMapper.selectByPrimaryKey(commentId);
    }

    @Override
    public void delete(Long id) {
        strategyCommentMapper.deleteByPrimaryKey(id);
    }
}
